﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using RoketSolutions.SP2010.ImageRotatorWebPart.Data.Entities;
using RoketSolutions.SP2010.ImageRotatorWebPart.Data.Repositories.Interfaces;
using RoketSolutions.SP2010.ImageRotatorWebPart.WebControls;
using RoketSolutions.SP2010.ImageRotatorWebPart.WebParts.ImageRotator;
using RoketSolutions.SP2010.Infrostructure.Data.Repositories;
using RoketSolutions.SP2010.Infrostructure.Security;
using RoketSolutions.SP2010.Infrostructure.UI.Controls;
using System.Linq;
using RoketSolutions.SP2010.Infrostructure.Common.Extensions;

namespace RoketSolutions.SP2010.ImageRotatorWebPart.ControlTemplates
{
    public partial class ImageRotatorControl : LocalizableUserControl, IImageRotatorControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindControls();
        }

        #region / Properties /

        public ImageRotatorSettings Settings { get; set; }

        #endregion

        #region / Methods /

        protected string GetOptionsJson()
        {
            return new JavaScriptSerializer().Serialize(Settings);
        }

        private void BindControls()
        {
            var data = RetrieveData();
            if (data.Any())
            {
                Visible = true;
                RepeaterSliders.DataSource = data;
                RepeaterSliders.DataBind();
            }
            else
            {
                Visible = false;
            }
        }

        private IEnumerable<ImageRotatorItem> RetrieveData()
        {
            var result = new List<ImageRotatorItem>();
            AdminContext.Do(SPContext.Current.Web, (site, web)=>
                                                       {
                                                           var repository = RepositoryLocator.Get<IImageRotatorRepository>(web);
                                                           result.AddRange(repository.FindAll());
                                                       });
            return result;
        }

        #endregion

        #region / Controls Event Handlers /

        protected void OnRepeaterSlidersItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var dataItem = e.Item.DataItem as ImageRotatorItem;
            if (dataItem != null)
            {
                var hpkItem = e.Item.FindControl("HyperLinkItem") as HyperLink;
                var imgItem = e.Item.FindControl("ImageItem") as Image;
                if (hpkItem != null && imgItem != null)
                {
                    if (!dataItem.LinkUrl.IsNullOrEmpty())
                    {
                        hpkItem.NavigateUrl = dataItem.LinkUrl;
                        hpkItem.Target = dataItem.LinkTarget == TargetType.Self ? "_self" : "_blank";
                    }
                    imgItem.ImageUrl = dataItem.ImageUrl;
                }
            }
        }

        #endregion

    }
}
